package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;

@Controller
@RequestMapping("strategy")
public class StrategyController {

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IStrategyThemeService strategyThemeService;

    @Autowired
    private IStrategyCatalogService strategyCatalogService;


    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") StrategyQuery qo, Model model) {

        Page<Strategy> page = strategyService.query(qo);

        model.addAttribute("page", page);

        return "strategy/list";
    }

    @RequestMapping("/input")
    public String input(String id, Model model) {

        if (StringUtils.hasLength(id)) {
            // 回显攻略
            model.addAttribute("strategy", strategyService.get(id));
        }

        model.addAttribute("catalogs", strategyCatalogService.queryGroupCatalog());

        model.addAttribute("themes", strategyThemeService.list());

        return "strategy/input";
    }


    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Strategy strategy) {

        strategyService.saveOrUpdate(strategy);

        return JsonResult.success();
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String id) {

        strategyService.delete(id);
        return JsonResult.success();
    }


    /**
     * 攻略 上架/发布 接口
     * @param id 攻略id
     * @param state 状态 0待发布 1发布
     * @return 攻略对象
     */
    @RequestMapping("/changeState")
    @ResponseBody
    public Object changeState(String id, int state) {

        return JsonResult.success(strategyService.changeState(id, state));
    }

}
